import type { GetFoodListParamsI } from '@/types/food'
import { cloudDBZone, initCloudDB } from '@/AGC/manager'
import { CloudDBZoneQuery } from '@hw-agconnect/database'
import { FoodFilters } from '@/AGC/model/FoodFilters'
import { Food } from '@/AGC/model/Food'
import type { UserFiltersI } from '@/types/user'

/**
 * 根据食物名称查询食物
 * @param name 食物名称
 * @returns 食物列表
 */
export const queryFoodListByName = async (name: string) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(Food)
  query.contains('name', name)
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 根据食物ids查询食物
 * @param ids 食物ids
 * @returns 食物集合
 */
export const queryFoodListByIds = async (ids: number[]) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(Food)
  query.in('id', ids).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 获取食物筛选条件集合--分页
 * @param foodFilter 筛选条件
 * @param ids 食物ids
 * @returns 食物筛选条件集合
 */
export const queryFoodFilters = async (foodFilter: GetFoodListParamsI, ids?: number[]) => {
  if (!cloudDBZone) await initCloudDB()

  // 分页查询的偏移量
  const offset = (foodFilter.page - 1) * foodFilter.pageSize
  // 获取 CloudDBZoneQuery 对象
  const query = CloudDBZoneQuery.where(FoodFilters)

  // ids: 根据食物名称查询出来的所有食物id 集合
  if (ids) {
    query.in('food_id', ids)
  }

  if (foodFilter.time == '早餐') {
    query.equalTo('breakfast', 1)
  } else if (foodFilter.time == '午餐') {
    query.equalTo('lunch', 1)
  } else if (foodFilter.time == '晚餐') {
    query.equalTo('dinner', 1)
  }

  query.limit(foodFilter.pageSize, offset)

  const snapshot = await cloudDBZone.executeQuery(query)
  // 获取食物筛选条件集合
  return snapshot.getSnapshotObjects()
}

/**
 * 获取食物筛选条件总数
 * @param foodFilter 筛选条件
 * @param ids 食物ids
 * @returns 食物筛选条件总数
 */
export const queryFoodTotal = async (foodFilter: GetFoodListParamsI, ids?: number[]) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(FoodFilters)

  if (ids) {
    query.in('food_id', ids)
  }

  if (foodFilter.time == '早餐') {
    query.equalTo('breakfast', 1)
  } else if (foodFilter.time == '午餐') {
    query.equalTo('lunch', 1)
  } else if (foodFilter.time == '晚餐') {
    query.equalTo('dinner', 1)
  }

  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects().length
}

/**
 * 获取食物筛选条件集合--不分页
 * @param filter 筛选条件
 * @returns 食物筛选条件集合
 */
export const queryFoodFiltersNotLimit = async (filter: UserFiltersI) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(FoodFilters)

  if (filter.target == 0) {
    query.equalTo('weight_agin', 1)
  } else {
    query.equalTo('reducing_fat', 1)
  }

  if (filter.eat_scene == 0) {
    query.equalTo('canteen', 1)
  } else if (filter.eat_scene == 1) {
    query.equalTo('home', 1)
  } else {
    query.equalTo('takeout', 1)
  }

  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}
